'use strict';

stareal
    .controller("IntegralController", function ($scope, $api,$state, $alert,$stateParams,localStorageService) {
        if($stateParams.app==1){
            $scope.titleStatus=true;
        }
        if($stateParams.accessToken==undefined||$stateParams.accessToken==null||$stateParams.accessToken==''){
            $scope.accessToken= localStorageService.get('token');
            var  rs = "main.integral-" + JSON.stringify({app:'',accessToken:'',checked:''});
            var token=localStorageService.get('token');
            var isbind = localStorageService.get('isbind');
            var openID = localStorageService.get('openid');
            var ua = window.navigator.userAgent.toLowerCase();
            if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                if (isbind != 1 && openID != '') {
                    localStorageService.set('cleartoken', token);
                    location.href = "#/main/register/" + encodeURIComponent(rs);
                    localStorageService.set('token', '');
                    return false
                }
            }
            if (!localStorageService.get('token')) {
                if (ua.match(/MicroMessenger/i) == 'micromessenger') {
                    // 正式地址
                    location.href = "https://open.weixin.qq.com/connect/oauth2/authorize?" +
                        "appid=wxda73ac8ac7af1261&" +
                        "redirect_uri=http%3A%2F%2Ftest.djx.m.opentickets.cn%2Foauth%2Findex" +
                        "&response_type=code&scope=snsapi_userinfo&state="+encodeURIComponent(rs);
                } else {
                    location.href = "#/main/login/"+encodeURIComponent(rs);
                }
                return false;
            }
        }else{
            $scope.accessToken=$stateParams.accessToken;
        }
        //安卓 点击事件拦截  checked
        if($stateParams.checked==undefined||$stateParams.checked==null||$stateParams.checked==''){
            $scope.androidStatus=false;
        }else{
            $scope.androidStatus=true;
        }
        $api.get("app/integral/intgralInfo", {accessToken:$scope.accessToken},false)
            .then(function (ret) {
                $scope.user = ret.data;
            });
        //获取会员信息
        $scope.GetMember = function () {
            $api.get("app/member/index/retrieve",{accessToken:$scope.accessToken},false)
                .then(function (ret) {
                    //console.log(ret)
                    $scope.level_name = ret.data.level_name
                    $scope.member = ret.data.level;
                    if($scope.member==1||$scope.member==2){
                        $scope.member_icon= "vip0";
                    }else if($scope.member==3||$scope.member==4){
                        $scope.member_icon= "vip1";
                    }else if($scope.member==5||$scope.member==6){
                        $scope.member_icon= "vip2";
                    }else{
                        $scope.member_icon= "vip3";
                    }
                    $scope.totalvalue = ret.data.up_value + ret.data.value; //该等级需要的总积分
                    $scope.up_value = ret.data.up_value;
                    $scope.value = ret.data.value;
                })
        }
        $scope.GetMember()

        //前往充值
        $scope.goToCharge=function () {
            if($scope.androidStatus) {
                android.ClickToWebPay();
            }else{
                $state.go('main.rechargewallet',{},true);
            }
        }
        //如何升级
        $scope.goToUpgrade=function(){
            if($scope.androidStatus){
              android1.ClickToWeb();
            }else{
                $state.go('main.integralGuide',{},true);
            }
        }
    });